<?php

/*
 * payment controller
 * create 2013/03/11
 * creator : Nguyen Van Nhieu
 */

class PaymentController extends AppController{
	var $name = 'Payment';
	var $uses = 'BankInformation';
	
	public function beforeFilter() {
		parent::beforeFilter();
		
	}
	
	/**
	 * index action
	 */
	function index(){
		$this->redirect('setting');
	}
	
	function setting(){
		$this->loadModel('AccountType');
		$accountTypes = $this->AccountType->find('list', array('fields' => array('id', 'account_type')));
		$userId = $this->CustomAuth->user('id');
		if(!empty($this->data)){
			$this->BankInformation->set($this->data);
			if($this->BankInformation->validates()){
				$this->BankInformation->set('user_id', $userId);
				if($this->Session->read('User.paymentAction') == 'edit'){
					$this->BankInformation->set('id', $this->Session->read('User.bankInforId'));
				}
				if($this->BankInformation->save()){
					$this->Session->write('User.paymentAction', 'edit');
					$this->Session->write('User.bankInforId', $this->BankInformation->id);
					$this->Session->setFlash(Messages::successfully, 'flash_success');
				}else{
					$this->Session->setFlash(Messages::error_please_try_again, 'flash_failure');
				}
			}
		}else{
			$bankInformation = $this->BankInformation->find('first', array('conditions' => array('user_id' => $userId)));
			$this->data = $bankInformation;
			if($bankInformation){
				$this->Session->write('User.paymentAction', 'edit');
				$this->Session->write('User.bankInforId', $bankInformation['BankInformation']['id']);
			}else{
				$this->Session->write('User.paymentAction', 'add');
			}
		}
		$this->set(array(
			'accountTypes' => $accountTypes,
			'title_for_layout' => '銀行口座情報の編集 - Adams Campus'
			));
	}
}
?>
